package com.scu.servlet;

import com.alibaba.fastjson.JSON;
import com.scu.dao.mPostDao;
import com.scu.entity.mPost;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "PostServlet", value = "/PostServlet")
public class PostServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<mPost> list = new mPostDao().findAll();

            String listStr = JSON.toJSONString(list);

            PrintWriter out = response.getWriter();
            out.print(listStr);
            out.flush();
            out.close();
            System.out.println("1");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
